package com.jinlingxiu.app.mvp.ui.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle
import com.afollestad.materialdialogs.MaterialDialog
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.Utils
import com.jaeger.library.StatusBarUtil
import com.jinlingxiu.app.R
import com.jinlingxiu.app.mvp.contract.PermissionContract
import com.jinlingxiu.app.mvp.presenter.PermissionPresenter
import com.tbruyelle.rxpermissions3.RxPermissions
import com.xdja.easymvp.base.BaseActivity
import io.reactivex.rxjava3.kotlin.subscribeBy
import org.koin.androidx.scope.lifecycleScope
import java.util.*

/**
 * 描述:
 * Create by yuanwanli
 * Date 2020/09/14
 */
class PermissionActivity : BaseActivity<PermissionPresenter>(), PermissionContract.View {
    override var mPresenter: PermissionPresenter? = lifecycleScope.get()

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_permission


    override fun initData(savedInstanceState: Bundle?) {
        StatusBarUtil.setTransparent(this)
        StatusBarUtil.setLightMode(this)
    }

    override fun onStart() {
        super.onStart()
        RxPermissions(this)
            .requestEachCombined(
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
            )
            .subscribeBy(
                onNext = { permission ->
                    // 获取到所需权限
                    if (permission.granted) {
                        mPresenter?.getDelay()
                    } else {
                        val content = String.format(
                            Locale.getDefault(), "在设置-应用-%s-权限中开启权限，以正常使用%s功能",
                            AppUtils.getAppName(), AppUtils.getAppName()
                        )
                        MaterialDialog(this)
                            .title(R.string.tips)
                            .message(text = content)
                            .show {
                                positiveButton(R.string.setting) { dialog ->
                                    val intent =
                                        IntentUtils.getLaunchAppDetailsSettingsIntent(Utils.getApp().packageName)
                                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                                    Utils.getApp().startActivity(intent)
                                    dialog.dismiss()
                                }
                                negativeButton(R.string.cancel) { dialog ->
                                    AppUtils.exitApp()
                                    dialog.dismiss()
                                }
                            }
                    }
                },
                onError = {

                }
            )
    }

    override fun startNext() {
        startActivity(Intent(this, WelcomeActivity::class.java))
        finish()
    }

    override fun showTips() {
        MaterialDialog(this)
            .title(R.string.tips)
            .message(text = "当前版本是测试版本,请联系开发人员获取正式版本")
            .show()
    }
}
